#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>

#define PORT 9000
#define IPADDRESS "127.0.0.95"

static void
error(char *msg)
{
	perror(msg);
	exit(1);
}

int main(int argc, char **argv)
{
	int z;				// exit status code 
	struct sockaddr_in adr_inet;	// Socket address
	int sck_inet;			// Socket fd
	int len_inet;			// Socket length

	// Create socket 
	sck_inet = socket(AF_INET, SOCK_STREAM, 0);
	if(sck_inet == -1)
		error("socket()");

	// establish address
	bzero((char *) &adr_inet, sizeof(adr_inet));
	adr_inet.sin_family = AF_INET;
	adr_inet.sin_port = htons(PORT);
	adr_inet.sin_addr.s_addr = inet_addr(IPADDRESS);

	if(adr_inet.sin_addr.s_addr == INADDR_NONE)
		error("bad address");

	len_inet = sizeof(adr_inet);

	// bind the socket
	z = bind(sck_inet, (struct sockaddr *) &adr_inet, len_inet);
	if(z == -1)
		error("bind()");

	listen(sck_inet, 5);

	// display out socket address
	system("netstat -npa --tcp 2> /dev/null | grep inetaddr");
	// system("lsof -i tcp | grep inetaddr");

	return 0;
}
